package com.jd.xubobo.bookshow.web;

import com.jd.xubobo.bookshow.dao.BookMapper;
import com.jd.xubobo.bookshow.domain.Book;
import com.jd.xubobo.bookshow.service.BookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class BookController {

    @Autowired
    private BookService bookService;

    //书籍列表，入参为页码编号
    @RequestMapping("/bookList/{page}")
    public String bookList(Model model, @PathVariable int page){
        List<Book> books = bookService.bookListByPage(page);
        model.addAttribute("books", books);
        return "bookList";
    }

    //根据书籍ID查找书籍，显示书籍详情
    @RequestMapping("/book/{id}")
    public String findById(Model model, @PathVariable long id){
        Book book = bookService.findById(id);
        model.addAttribute("book", book);
        return "bookDetail";
    }

    //根据书籍名字查找
    @RequestMapping(value = "/bookDetail", method = RequestMethod.POST)
    public String findByName(Model model,@RequestParam("name") String name){
        Book book = bookService.findByName(name);
        if (book == null)   return "error";
        model.addAttribute("book", book);
        return "bookDetail";
    }

    //判断书名是否存在，存在返回1，不存在返回0
    @RequestMapping("/isBookNameExist")
    @ResponseBody
    public int isBookNameExist(@RequestParam("bookName") String name){
        Book book = bookService.findByName(name);
        if (book != null)   return 1;
        else return 0;
    }

    //显示书籍修改页面
    @RequestMapping(value = "/bookUpdatePage/{id}", method = RequestMethod.GET)
    public String showUpdatePage(Model model, @PathVariable long id){
        Book book = bookService.findById(id);
        model.addAttribute("book", book);
        return "bookUpdate";
    }

    //修改书籍信息
    @RequestMapping(value = "/bookUpdate", method = RequestMethod.POST)
    public String  updateOne(Model model,
                             @RequestParam("id") long id,
                             @RequestParam("name") String name,
                             @RequestParam("price") double price,
                             @RequestParam("stock") int stock){
        Book book = new Book(id, name, price, stock);
        try {
            bookService.updateOne(book);
            model.addAttribute("book", book);
            return "bookDetail";
        }catch (Exception e){
            return "error";
        }

    }

    //删除书籍
    @RequestMapping(value = "/bookDelete/{id}")
    public String deleteOne(Model model, @PathVariable long id){
        boolean sucess = bookService.deleteOne(id);
        if (sucess){
            List<Book> books = bookService.bookListByPage(1);
            model.addAttribute("books", books);
            return "bookList";
        }
        else return "error";
    }

    //展示增加书籍页面
    @RequestMapping(value = "/bookAddPage", method = RequestMethod.GET)
    public String bookAddPage(){
        return "bookAdd";
    }

    //增加书籍
    @RequestMapping(value = "/bookAdd", method = RequestMethod.POST)
    public String addOne(Model model,
                         @RequestParam("name") String name,
                         @RequestParam("price") double price,
                         @RequestParam("stock") int stock){
        Book book = new Book(name,price,stock);
        try{
            boolean success = bookService.insertOne(book);
            if (success){
                System.out.println(name);
                book = bookService.findByName(name);
                model.addAttribute("book", book);
                return "bookDetail";
            } else {
                return "error";
            }
        }catch (Exception e){
            System.out.println("增加书籍信息时出现错误：书籍名字重复或其他位置错误");
            return "error";
        }
    }


    /*分页*/
    //上一页
    @RequestMapping("/previousPage")
    public String previousPage(Model model){
        List<Book> books = bookService.previousPage();
        model.addAttribute("books", books);
        return "bookList";
    }
    //下一页
    @RequestMapping("/nextPage")
    public String nextPage(Model model){
        List<Book> books = bookService.nextPage();
        model.addAttribute("books", books);
        return "bookList";
    }
    //最后一页
    @RequestMapping("/lastPage")
    public String lastPage(Model model){
        List<Book> books = bookService.lastPage();
        model.addAttribute("books", books);
        return "bookList";
    }
}
